-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
90 lines (67 loc) · 2.18 KB
/
code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
let wiggleShader;
let vertSrc = `
precision highp float;
attribute vec3 aPosition;
attribute vec4 aVertexColor;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
varying vec4 vVertexColor;
uniform float time;
void main() {
vec3 position = aPosition;
// Add an offset per vertex. There will be a time delay based
// on the texture coordinates.
position.y += 20.0 * sin(time * 0.01 + position.y * 0.1);
// Apply the transformations that have been set in p5
vec4 viewModelPosition = uModelViewMatrix * vec4(position, 1.0);
// Tell WebGL where the vertex should be drawn
gl_Position = uProjectionMatrix * viewModelPosition;
// Pass along the color of the vertex to the fragment shader
vVertexColor = aVertexColor;
}
`;
let fragSrc = `
precision highp float;
// Receive the vertex color from the vertex shader
varying vec4 vVertexColor;
void main() {
// Color the pixel with the vertex color
gl_FragColor = vVertexColor;
}
`;
let ribbon;
function setup() {
createCanvas(700, 400, WEBGL);
wiggleShader = createShader(vertSrc, fragSrc);
let startColor = color('#F55');
let endColor = color('#55F');
ribbon = buildGeometry(() => {
noStroke();
// Draw a ribbon of vertices
beginShape(QUAD_STRIP);
let numPoints = 50;
for (let currentPoint = 0; currentPoint < numPoints; currentPoint++) {
let x = map(currentPoint, 0, numPoints - 1, -width / 3, width / 3);
let y = map(currentPoint, 0, numPoints - 1, -height / 3, height / 3);
// Change color from red to blue along the ribbon
fill(lerpColor(startColor, endColor, currentPoint / (numPoints - 1)));
for (let z of [-50, 50]) {
vertex(x, y, z);
}
}
endShape();
});
describe('A red-to-blue ribbon that waves over time');
}
function draw() {
background(255);
noStroke();
rotateX(PI * 0.1);
// Use the vertex shader we made. Try commenting out this line to see what
// the ribbon looks like when we don't move it with the shader!
shader(wiggleShader);
// Pass the shader the current time so it can animate.
wiggleShader.setUniform('time', millis());
// Draw the ribbon. The shader will distort and animate it.
model(ribbon);
}